package com.wr.service.posts.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.wr.common.entity.TbPostsPost;
import com.wr.common.utils.BaseResult;
import com.wr.service.posts.service.PostsService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/posts")
public class PostsController {

    @Autowired
    private PostsService postsService;

    /**
     * 根据 ID 获取文章
     *
     * @param postGuid
     * @return
     */
    @ApiOperation(value = "通过id获取单条文章")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "postGuid",value = "文章主键id",required = true,dataType = "int", paramType = "path")
    })
    @RequestMapping(value = "/{postGuid}", method = RequestMethod.GET)
    public BaseResult get(@PathVariable String postGuid) {
        TbPostsPost tbPostsPost = new TbPostsPost();
        tbPostsPost.setPostGuid(postGuid);
        TbPostsPost obj = postsService.selectOne(tbPostsPost);
        return BaseResult.ok(obj);
    }


    /**
     * 保存文章
     *
     * @return
     */
    @RequestMapping(value = "/saveOrUpdate",method = RequestMethod.POST)
    public BaseResult save(
            @RequestParam(required = true) String tbPostsPostJson,
            @RequestParam(required = true) String optsBy
    ) {
        int result = 0;

        TbPostsPost tbPostsPost = null;

        tbPostsPost = JSON.parseObject(tbPostsPostJson, TbPostsPost.class);

        if (tbPostsPost != null) {

            // 新增文章
            if (StringUtils.isBlank(tbPostsPost.getPostGuid())) {
                tbPostsPost.setPostGuid(UUID.randomUUID().toString());
                result = postsService.insert(tbPostsPost, optsBy);
            }

            // 修改文章
            else {
                result = postsService.update(tbPostsPost, optsBy);
            }

            // 最少有一行数据受影响
            if (result > 0) {
                return BaseResult.ok("保存文章成功");
            }
        }

        return BaseResult.ok("保存文章失败");
    }


    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @param tbPostsPostJson
     * @return
     */
    @GetMapping("/page/{pageNum}/{pageSize}")
    public BaseResult page(@PathVariable int pageNum,
                           @PathVariable int pageSize,
                           @RequestParam(required = false) String tbPostsPostJson)
    {
        TbPostsPost tbPostsPost = null;
        if (StrUtil.isNotBlank(tbPostsPostJson)){
            tbPostsPost = JSON.parseObject(tbPostsPostJson, TbPostsPost.class);
        }
        PageInfo page = postsService.page(pageNum, pageSize, tbPostsPost);
        // 分页后的结果集
        List<TbPostsPost> list = page.getList();
        BaseResult.Cursor cursor = new BaseResult.Cursor();
        cursor.setTotal(new Long(page.getTotal()).intValue());
        // 当前页
        cursor.setOffset(page.getPageNum());
        // 每页条数
        cursor.setLimit(page.getPageSize());
        return BaseResult.ok(list,cursor);
    }
}
